package com.sunrise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunrise.mapper.GroupMapper;
import com.sunrise.pojo.system.Group;
import com.sunrise.service.GroupService;
import com.sunrise.vo.system.group.GroupPageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import result.PageGridResult;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: liansen
 * @Date: 2023-08-27  21:28
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    @Override
    public PageGridResult pageQuery(GroupPageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        String name = pageVO.getName();
        Byte status = pageVO.getStatus();
        LocalDateTime[] times = pageVO.getCreateTime();
        LambdaQueryWrapper<Group> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNoneBlank(name)){
            wrapper.like(Group::getName, name);
        }
        if(status != null){
            wrapper.eq(Group::getStatus, status);
        }
        if(times != null && times.length > 0){
            wrapper.between(Group::getCreateTime, times[0], times[1]);
        }
        wrapper.orderByDesc(Group::getCreateTime);
        List<Group> groups = baseMapper.selectList(wrapper);
        PageInfo<Group> pageInfo = new PageInfo<>(groups);
        PageGridResult result = new PageGridResult();
        result.setPage(pageVO.getPageNum());
        result.setRows(pageInfo.getList());
        result.setLimit(pageVO.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setPageCount(pageInfo.getPages());
        return result;
    }
}
